﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using PPI.Client;
using PPI.Client.Interface;
using PPI.Client.Logic;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Lookup;
using VMind.Global;

namespace PPI
{
    public partial class frmDeployDatabase : frmDeployBase
    {
        private List<EVendor> m_ListVendors;
        private List<EHandheld> m_ListHandheld;
        private List<EUtility> m_ListUtility;
        private List<EVendor_Project> m_ListProjects;
        private List<EVendor_Project> m_ListSelectedProjects;
        private List<EVendor_Contact> m_ListSelectedContacts;


        public frmDeployDatabase()
        {
            InitializeComponent();
            base.MenuMain.Visible = false;
            this.FormSize = FormSizes.Medium;
            try
            {
                
                LoadCombos();

            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void LoadCombos()
        {
            m_ListVendors = LCacheDataSingleton.Instance.Vendor;
            m_ListHandheld = LCacheDataSingleton.Instance.Handhelds;
            m_ListUtility = LCacheDataSingleton.Instance.Utilities;
            m_ListProjects = LCacheDataSingleton.Instance.Projects;


            projectView1.ListProjects = m_ListProjects;
            DropdownHelper.FillDropDown(ddlUtility, m_ListUtility, typeof(EUtility), "Name", "UtilityId", "Select an option...");
            DropdownHelper.FillDropDown(ddlVendor, m_ListVendors, typeof(EVendor), "Name", "VendorId", "Select an option...");
            DropdownHelper.FillDropDown(ddlHandheld, m_ListHandheld, typeof(EHandheld), "Name", "Id", "Select an option...");
            addingHandheld1.CurrentHandhelds = m_ListHandheld;



        }

        #region Control Events

        private void ddlUtility_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }

        private void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }

        private void ddlHandheld_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }
        
        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProject.Enabled = (ddlVendor.SelectedIndex > 0);
            ddlUser.Enabled = (ddlVendor.SelectedIndex > 0);

            int _selectedValue = 0;

            //Load de ProjectCombo
            if (ddlVendor.SelectedIndex > 0)
            {
                ListBoxItem _selectedItem = (ListBoxItem)ddlVendor.SelectedItem;

                _selectedValue = ConvertHelper.ToInt32(_selectedItem.Value);

                m_ListSelectedProjects = m_ListVendors.Find(delegate(EVendor v) { return v.Id == _selectedValue; }).Projects;
                DropdownHelper.FillDropDown(ddlProject, m_ListSelectedProjects, typeof(EVendor_Project), "Name", "Vendor_ProjectId", "Select an option...");

                m_ListSelectedContacts = m_ListVendors.Find(delegate(EVendor v) { return v.Id == _selectedValue; }).Contacts;
                DropdownHelper.FillDropDown(ddlUser, m_ListSelectedContacts, typeof(EVendor_Contact), "Name", "Vendor_ContactId", "Select an option...");
            }
            ActivateDeploy();
        }

        private void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _selectedValue = 0;
            projectView1.Visible = false;
            projectView1.SelectedProjectId = Int32.MinValue;
            if (ddlProject.SelectedIndex > -1)
            {
                ListBoxItem _selectedItem = (ListBoxItem)ddlProject.SelectedItem;
                _selectedValue = ConvertHelper.ToInt32(_selectedItem.Value);
                projectView1.Visible = true;
                projectView1.SelectedProjectId = _selectedValue;
                projectView1.LoadData();
            }
            ActivateDeploy();
        }

        private void addingHandheld1_FinishInsert(object sender, EventArgs e)
        {
            ddlHandheld.Items.Clear();
            DropdownHelper.FillDropDown(ddlHandheld, addingHandheld1.CurrentHandhelds, typeof(EHandheld), "Name", "Id", "Select an option...");
            this.addingHandheld1.Hide();

        }

        private void btnAddHandheld_Click(object sender, EventArgs e)
        {
            this.addingHandheld1.Show();
        }



        #endregion

        private void restartComboIndex()
        {
            DropdownHelper.ResetComboIndex(ddlUtility);
            DropdownHelper.ResetComboIndex(ddlVendor);
            DropdownHelper.ResetComboIndex(ddlProject);
            DropdownHelper.ResetComboIndex(ddlUser);
            DropdownHelper.ResetComboIndex(ddlHandheld);
        }

        protected override void DoNewDeploy()
        {
            if (MessageBox.Show("Would you like to do another 'gps visit' deployment?", "New Deploy", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                this.Close();
            else
                restartComboIndex();

        }

        protected override void DoDeploy()
        {
            if (ValidateAll())
            {
                ListBoxItem _selectedItem = (ListBoxItem)ddlUtility.SelectedItem;
                EUtility _Utility = new EUtility(ConvertHelper.ToInt32(_selectedItem.Value), ddlUtility.Text);
                _selectedItem = (ListBoxItem)ddlProject.SelectedItem;
                EVendor_Project _Project = new EVendor_Project(ConvertHelper.ToInt32(_selectedItem.Value), ddlProject.Text);
                _selectedItem = (ListBoxItem)ddlHandheld.SelectedItem;
                EHandheld _Handheld = new EHandheld(ConvertHelper.ToInt32(_selectedItem.Value), ddlHandheld.Text);
                _selectedItem = (ListBoxItem)ddlUser.SelectedItem;
                EVendor_Contact _Contact = new EVendor_Contact(ConvertHelper.ToInt32(_selectedItem.Value), ddlUser.Text);
                _selectedItem = (ListBoxItem)ddlVendor.SelectedItem;
                EVendor _Vendor = new EVendor(ConvertHelper.ToInt32(_selectedItem.Value), ddlVendor.Text, "");
                string sourceDatabase;
                try
                {
                    DeployDatabasePresenter logicDatabase = (DeployDatabasePresenter) m_Presenter;
                    sourceDatabase = logicDatabase.DeployGPS(_Utility, _Project, _Handheld, _Contact, _Vendor);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                DirectoryInfo _DestinationFolder = SelectDestinationFolder();
                FileInfo _FileToCopy;

                if (_DestinationFolder == null)
                {
                    return;
                }
                m_Presenter.DownloadFile(sourceDatabase, _DestinationFolder + "\\" + Constants.K_LOCALDATABASEGPS);
                m_Presenter.CopyLocalFilesToDestination(_DestinationFolder.FullName);
            }
        }

        private void frmDeployDatabase_FormClosing(object sender, FormClosingEventArgs e)
        {
            restartComboIndex();
        }

        private void frmDeployDatabase_Load(object sender, EventArgs e)
        {
            try
            {
                m_Presenter = new DeployDatabasePresenter(this);
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

    }
}
